package org.cainiao.oauth2.client.springboot.oauth2client;

import org.cainiao.oauth2.client.core.component.DaoClientRegistrationRepository;
import org.cainiao.oauth2.client.core.dao.service.CnClientRegistrationMapperService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;

/**
 * <br />
 * <p>
 * Author: Cai Niao(wdhlzd@163.com)<br />
 */
@Configuration(proxyBeanMethods = false)
public class CNAuthorizationCenterOAuth2ClientRegistrationRepositoryConfiguration {

    /**
     * 如果没有配置 ClientRegistrationRepository @Bean<br />
     * OAuth2ClientAutoConfiguration @Import 的 OAuth2ClientRegistrationRepositoryConfiguration 会根据<br />
     * OAuth2ClientProperties（spring.security.oauth2.client）<br />
     * 自动配置一个 InMemoryClientRegistrationRepository<br />
     */
    @Bean
    @ConditionalOnMissingBean(ClientRegistrationRepository.class)
    ClientRegistrationRepository clientRegistrationRepository(
        CnClientRegistrationMapperService cnClientRegistrationMapperService) {

        return new DaoClientRegistrationRepository(cnClientRegistrationMapperService);
    }
}
